﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using YanZhiwei.DotNet2.Utilities.Common;
using YanZhiwei.DotNet2.Utilities.Enums;

namespace YanZhiwei.DotNet2.Utilities.Common.Tests
{
    [TestClass()]
    public class ByteHelperTests
    {
        [TestMethod()]
        public void MaxValueToPercentTest()
        {
            decimal _actual = ByteHelper.MaxValueToPercent(162);
            Assert.AreEqual(63.53m, _actual);
        }

        [TestMethod()]
        public void ToHexStringTest()
        {
            string _actual = ByteHelper.ToHexString(new byte[2] { 255, 254 }, ToHexadecimal.ConvertAll);
            Assert.AreEqual("FFFE", _actual);
        }

        [TestMethod()]
        public void ToHexStringWithBlankTest()
        {
            string _actual = ByteHelper.ToHexStringWithBlank(new byte[2] { 255, 254 });
            Assert.AreEqual("FF FE", _actual);
        }

        [TestMethod()]
        public void ToHexStrWithDelimiterTest()
        {
            string _actual = ByteHelper.ToHexStrWithDelimiter(new byte[2] { 255, 254 }, "-");
            Assert.AreEqual("FF-FE", _actual);
        }

        [TestMethod()]
        public void ConvertHexStringTest()
        {
            byte[] _actual = ByteHelper.ConvertHexString("FFFE");
            CollectionAssert.AreEqual(new byte[2] { 0xFF, 0xFE }, _actual);
        }

        [TestMethod()]
        public void ConvertHexStringWithDelimiterTest()
        {
            byte[] _actual = ByteHelper.ConvertHexStringWithDelimiter("00-03-00-B2", "-");
            CollectionAssert.AreEqual(new byte[4] { 0x00, 0x03, 0x00, 0xB2 }, _actual);
        }

        [TestMethod()]
        public void DTToBytesLen7Test()
        {
            byte[] _actual = ByteHelper.DTToBytesLen7(new DateTime(2015, 1, 05, 12, 12, 12));
            CollectionAssert.AreEqual(new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }, _actual);
        }

        [TestMethod()]
        public void DTToBytesLen6Test()
        {
            byte[] _actual = ByteHelper.DTToBytesLen6(new DateTime(2015, 1, 05, 12, 12, 12));
            CollectionAssert.AreEqual(new byte[6] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12 }, _actual);
        }

        [TestMethod()]
        public void DTToBytesLen4Test()
        {
            byte[] _actual = ByteHelper.DTToBytesLen4(new DateTime(2015, 1, 05, 12, 12, 12));
            CollectionAssert.AreEqual(new byte[4] { 0x20, 0x15, 0x01, 0x05 }, _actual);
        }

        [TestMethod()]
        public void TimeToBytesLen2Test()
        {
            byte[] _actual = ByteHelper.TimeToBytesLen2("14:56");
            CollectionAssert.AreEqual(new byte[2] { 0x14, 0x56 }, _actual);
        }

        [TestMethod()]
        public void DTToBytesLen7InvertedTest()
        {
            byte[] _actual = ByteHelper.DTToBytesLen7Inverted(new DateTime(2015, 1, 05, 12, 12, 12));
            CollectionAssert.AreEqual(new byte[7] { 0x12, 0x12, 0x12, 0x05, 0x01, 0x15, 0x20 }, _actual);

        }

        [TestMethod()]
        public void DTToBytesLen6InvertedTest()
        {
            byte[] _actual = ByteHelper.DTToBytesLen6Inverted(new DateTime(2015, 1, 05, 12, 12, 12));
            CollectionAssert.AreEqual(new byte[6] { 0x12, 0x12, 0x12, 0x05, 0x01, 0x15 }, _actual);
        }

        [TestMethod()]
        public void GetBitTest()
        {
            int _actual = ByteHelper.GetBit(8, 3);
            Assert.AreEqual(1, _actual);
        }

        [TestMethod()]
        public void SetBitTest()
        {
            byte _actual = ByteHelper.SetBit(8, 4);
            Assert.AreEqual(24, _actual);
        }

        [TestMethod()]
        public void ClearBitTest()
        {
            byte _actual = ByteHelper.ClearBit(24, 4);
            Assert.AreEqual(8, _actual);
        }

        [TestMethod()]
        public void ReverseBitTest()
        {
            byte _actual = ByteHelper.ReverseBit(24, 4);
            Assert.AreEqual(8, _actual);
        }

        [TestMethod()]
        public void BinaryToByteTest()
        {
            byte _actual = ByteHelper.BinaryToByte("11111111");
            Assert.AreEqual(255, _actual);
        }

        [TestMethod()]
        public void BytesToIntExHLTest()
        {
            int _actual = ByteHelper.BytesToIntExHL(new byte[2] { 0xff, 0xfe }, 0, 2);
            Assert.AreEqual(65279, _actual);
        }

        [TestMethod()]
        public void BytesToHexExHLTest()
        {
            string _actual = ByteHelper.BytesToHexExHL(new byte[2] { 0xff, 0xfe }, 0, 2);
            Assert.AreEqual("FEFF", _actual);
        }

        [TestMethod()]
        public void BytesInvertedToDTTest()
        {
            DateTime _acutal = ByteHelper.BytesInvertedToDT(new byte[7] { 0x12, 0x12, 0x12, 0x05, 0x01, 0x15, 0x20 }, 0, 7);
            Assert.AreEqual(new DateTime(2015, 1, 05, 12, 12, 12), _acutal);
        }

        [TestMethod()]
        public void BytesLen6ToDTTest()
        {
            DateTime _acutal = ByteHelper.BytesLen6ToDT(new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }, 0, 6);
            Assert.AreEqual(new DateTime(2015, 01, 05, 12, 12, 00), _acutal);

        }

        [TestMethod()]
        public void BytesLen7ToDTTest()
        {
            DateTime _acutal = ByteHelper.BytesLen7ToDT(new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }, 0, 7);
            Assert.AreEqual(new DateTime(2015, 01, 05, 12, 12, 12), _acutal);
        }

        [TestMethod()]
        public void BytesLen4ToDTTest()
        {
            DateTime _actual = ByteHelper.BytesLen4ToDT(new byte[4] { 0x20, 0x12, 0x10, 0x12 }, 0, 4);
            Assert.AreEqual(new DateTime(2012, 10, 12), _actual);
        }

        [TestMethod()]
        public void BytesLen2ToTimeTest()
        {
            string _actual = ByteHelper.BytesLen2ToTime(new byte[2] { 0x17, 0x45 }, 0, 2);
            Assert.AreEqual("17:45", _actual);
        }

        [TestMethod()]
        public void BytesToHexStringTest()
        {
            string _actual = ByteHelper.BytesToHexString(new byte[2] { 255, 254 }, 0, 2);
            Assert.AreEqual("FFFE", _actual);
        }

        [TestMethod()]
        public void ByteToHexWithBlankTest()
        {
            string _actual = ByteHelper.ByteToHexWithBlank(new byte[2] { 255, 254 }, 0, 2);
            Assert.AreEqual("FF FE", _actual);
        }

        [TestMethod()]
        public void BytesToIntTest()
        {
            int _actual = ByteHelper.BytesToInt(new byte[2] { 255, 254 }, 0, 2);
            Assert.AreEqual(65534, _actual);
        }

        [TestMethod()]
        public void ToByteTest()
        {
            byte _actual = ByteHelper.ToByte(254);
            Assert.AreEqual(254, _actual);
        }

        [TestMethod()]
        public void PercentToByteTest()
        {
            byte _actual = ByteHelper.PercentToByte(70);
            Assert.AreEqual(178, _actual);
        }

        [TestMethod()]
        public void ToBytesTest()
        {
            byte[] _actual = ByteHelper.ToBytes(258);
            CollectionAssert.AreEqual(new byte[2] { 0x01, 0x02 }, _actual);
        }

        [TestMethod()]
        public void ToBytesExHLTest()
        {
            byte[] _actual = ByteHelper.ToBytesExHL("FEFF");
            CollectionAssert.AreEqual(new byte[2] { 0xFF, 0xFE }, _actual);
        }

        [TestMethod()]
        public void ToBytesExHLTest1()
        {
            byte[] _actual = ByteHelper.ToBytesExHL(258);
            CollectionAssert.AreEqual(new byte[2] { 0x02, 0x01 }, _actual);
        }

        [TestMethod()]
        public void ToBytesLen2ExHLTest()
        {
            byte[] _actual = ByteHelper.ToBytesLen2ExHL(258);
            CollectionAssert.AreEqual(new byte[2] { 0x02, 0x01 }, _actual);
        }

        [TestMethod()]
        public void ReverseBinaryToByteTest()
        {
            byte _actual = ByteHelper.ReverseBinaryToByte("11110000");
            Assert.AreEqual(15, _actual);
        }

        [TestMethod()]
        public void BytesToBinaryStringTest()
        {
            string _actual = ByteHelper.BytesToBinaryString(new byte[1] { 0xff }, 0, 1);
            Assert.AreEqual("11111111", _actual);
        }

        [TestMethod()]
        public void BinaryStringToBytesTest()
        {
            byte[] _actual = ByteHelper.BinaryStringToBytes("1111111111111111");
            CollectionAssert.AreEqual(new byte[2] { 0xff, 0xff }, _actual);
        }

        [TestMethod()]
        public void OverPointValueUseReversedTest()
        {
            int _actual = ByteHelper.OverPointValueUseReversed(new byte[2] { 0xff, 0xfe }, new byte[2] { 0xff, 0xff });
            Assert.AreEqual(65534, _actual);
        }
    }
}
